package krt.wid.gslzs.util;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;

import java.util.ArrayList;

/**
 * @author Marcus
 * @package krt.wid.tour_gz.util
 * @description
 * @time 2017/6/11
 * 图片选择二次封装
 */

public class ImagePickerUtil {
    private ImagePicker mImagePicker;

    public ImagePickerUtil() {
        mImagePicker = ImagePicker.getInstance();
        mImagePicker.setImageLoader(new GlideImageLoader());
    }

    private void config(Context context, ArrayList<ImageItem> images, boolean crop) {
        mImagePicker.setStyle(CropImageView.Style.CIRCLE);
        Integer radius = Integer.valueOf("140");
        radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, context.getResources().getDisplayMetrics());
        mImagePicker.setFocusWidth(radius * 2);
        mImagePicker.setFocusHeight(radius * 2);
        mImagePicker.setCrop(crop);
        mImagePicker.setOutPutX(Integer.valueOf("800"));
        mImagePicker.setOutPutY(Integer.valueOf("800"));

    }

    /**
     * 单选
     *
     * @param context
     * @param images
     */
    public void singlePick(Context context, ArrayList<ImageItem> images) {
        mImagePicker.setMultiMode(false);
        config(context, images, true);
        Intent intent = new Intent(context, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, images);
        ((Activity) context).startActivityForResult(intent, 100);
    }

    /**
     * 多选
     *
     * @param context
     * @param images
     */
    public void multiplePick(Context context, ArrayList<ImageItem> images, int MaxCount, String mode) {
        mImagePicker.setMultiMode(true);
        mImagePicker.setSelectLimit(MaxCount); //选择最大张书
        config(context, images, false);
        Intent intent = new Intent(context, ImageGridActivity.class);
        if (mode.equals(ImageGridActivity.EXTRAS_IMAGES)) {
            intent.putExtra(mode, images);
        } else {
            intent.putExtra(mode, true);
        }

        ((Activity) context).startActivityForResult(intent, 100);

    }

    public void multiplePick(Context context, ArrayList<ImageItem> images, String mode) {
        this.multiplePick(context, images, 9, mode);

    }
}
